package com.google.code.fluentjoiner.io.local;

import com.google.code.fluentjoiner.IteratorSupplierBuilder;
import com.google.code.fluentjoiner.io.FileObject;
import com.google.code.fluentjoiner.io.FileObjectSupplierBuilder;
import com.google.common.base.Function;
import com.google.common.collect.Iterators;
import java.io.File;
import java.util.Arrays;
import java.util.Iterator;

public abstract class LocalFileObjectSupplierBuilder extends FileObjectSupplierBuilder
{
    private static final Function<File, LocalFileObjectSupplierBuilder> FILE_TO_FILEOBJECTSUPPLIER_BUILDER_FUNCTION = new Function<File, LocalFileObjectSupplierBuilder>()
    {
        @Override
        public LocalFileObjectSupplierBuilder apply(final File from)
        {
            return LocalFileObjectSupplierBuilder.fromPath(from.getAbsolutePath());
        }
    };
    
    public static LocalFileObjectSupplierBuilder fromLocalFileObject(final LocalFileObject localFileObject)
    {
        return new LocalFileObjectSupplierBuilder()
        {
            @Override
            protected FileObject getSupplierResult()
            {
                return localFileObject;
            }
        };        
    }

    public static LocalFileObjectSupplierBuilder fromPath(final String path)
    {
        return fromLocalFileObject(new LocalFileObject(new File(path)));
    }

    public static IteratorSupplierBuilder<LocalFileObjectSupplierBuilder> filesInFolder(final String path)
    {
        return new IteratorSupplierBuilder<LocalFileObjectSupplierBuilder>()
        {
            @Override
            protected Iterator<LocalFileObjectSupplierBuilder> getSupplierResult()
            {
                return Iterators.transform(Arrays.asList(new File(path).listFiles()).iterator(), FILE_TO_FILEOBJECTSUPPLIER_BUILDER_FUNCTION);
            }
        };
    }
}